package com.zanfeng.signleplatform.config.advices;

import com.zanfeng.signleplatform.config.auth.AuthUserResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class WtWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new ApiAuthorizationInterceptor()).addPathPatterns("/api/**");
        registry.addInterceptor(new ModulesAuthorizationInterceptor()).addPathPatterns("/modules/**");
    }


    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new AuthUserResolver());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //  测试用，使用当前web资源目录作为临时的文件存储目录，正式环境不应该这样使用
        registry.addResourceHandler("/files/**").addResourceLocations("file:files/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

}
